﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sigecov.Models;
using Sigecov.Formularios;
namespace Sigecov.Formularios
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
        public void LimparLogin()
        {
            txtUsuario.Clear();
            txtSenha.Clear();
        }
       
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            Pn3.Location = new Point((this.Size.Width / 2) - (this.Pn3.Size.Width / 2), (this.Size.Height / 2) - (this.Pn3.Size.Height / 2));
            pn4.Width = this.Width * 3;
            pn2.Width = this.Width * 3;
            txtUsuario.Text = "1000";
            txtSenha.Text = "123";
            btnLogin.Focus();
        }

        private void btnSair_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void btnLogin_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (!(string.IsNullOrWhiteSpace(txtUsuario.Text) || (string.IsNullOrWhiteSpace(txtSenha.Text))))
                {

                    using (SigecovContext DB = new SigecovContext())
                    {
                        var sql = DB.Usuarios.Select(u => new { u.usuId, u.usuLogin, u.usuSenha, u.usuTipoAcesso }).Where(u => u.usuLogin.Equals(txtUsuario.Text) && u.usuSenha.Equals(txtSenha.Text));
                        if (sql.Count() > 0)
                        {
                            FrmPrincipal sistema = new FrmPrincipal();
                            Usuario usuario = new Usuario();

                            /*foreach(var item in sql){
                                usuario.usuId               = item.usuId;
                                usuario.usuLogin            = item.usuLogin;
                                usuario.usuTipoAcesso       = item.usuTipoAcesso;
                                sistema._usuarioLogado = usuario;
                            }*/
                            sistema.Show();
                            LimparLogin();
                            this.Visible = false;
                        }
                        else
                        {
                            lblErroLogin.Text = "Usuário ou senha inválidos!";
                            LimparLogin();
                            txtUsuario.Focus();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Informe usuário e senha!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    if (string.IsNullOrWhiteSpace(txtUsuario.Text))
                        txtUsuario.Focus();
                    else if (string.IsNullOrWhiteSpace(txtSenha.Text))
                        txtSenha.Focus();
                    else
                        txtUsuario.Focus();
                }
            }
            catch
            {
                MessageBox.Show("Ocorreu um erro durante o login, favor informar a administração", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
